package com.sharemarking.application.common.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sharemarking.application.common.BaseSpecification;
import com.sharemarking.application.common.entity.User;
import com.sharemarking.application.common.repository.UserRepo;

@Controller
@RequestMapping("user")
public class UserController {
	@Autowired
	private UserRepo dao;
	
	@RequestMapping(value="index",method=RequestMethod.GET)
	@PreAuthorize("hasPermission('sys:user',1)")
	public void index(User e,Integer page,Integer size,Model model){
		if(page==null)page=0;
		else page-=1;
		if(size==null)size=30;
		Sort order=new Sort(Direction.DESC,"id");
		PageRequest req=new PageRequest(page,size,order);
		Page<User> p=dao.findAll(new BaseSpecification<User>(e), req);
		model.addAttribute("page", p);
		model.addAttribute("search", e);
	}
	
	/**
	 * 获取实体接口
	 * @param e
	 * @return
	 */
	@RequestMapping(value="{id}",method=RequestMethod.GET)
	@PreAuthorize("hasPermission('sys:user',1)")
	public String get(User obj,Model model){
		if(obj.getId()!=0) obj=dao.findOne(obj.getId());
		model.addAttribute("obj",obj);
		return "user/form";
	}
	/**
	 * id==0  =>  新增实体
	 * id!=0  =>  更新实体
	 * @param e
	 * @return
	 */
	@RequestMapping(value="{id}",method=RequestMethod.POST)
	@Transactional
	@PreAuthorize("hasPermission('sys:user',#target.id==0?8:4)")
	public String post(User target,Model model){
		if(target.getRoles()!=null)target.getRoles().removeIf(r->r.getId()==null);
		if(target.getLoginFlag()==null)target.setLoginFlag(false);
		if(target.getId()==0){
			dao.save(target);
			return "redirect:index";
		}else {
			User e=dao.findOne(target.getId());
			if(e==null)return null;
			e.setAvatar(target.getAvatar());
			e.setEmail(target.getEmail());
			e.setMobile(target.getMobile());
			e.setName(target.getName());
			e.setRoles(target.getRoles());
			e.setLoginFlag(target.getLoginFlag());
			return "redirect:index";
		}
	}
	/**
	 * 逻辑删除
	 * @param id
	 */
	@RequestMapping(value="{id}/delete",method=RequestMethod.GET)
	@Transactional
	@PreAuthorize("hasPermission('sys:user',16)")
	public String delete(@PathVariable("id")Integer id){
		dao.findOne(id).setDelFlag(true);
		return "redirect:../index";
	}
	
	@RequestMapping(value="checkLoginName",method=RequestMethod.POST)
	@ResponseBody
	public String checkLoginName(String param){
		return dao.findByLoginName(param)!=null?"该帐号已存在":"y";
	}
}
